package cn.bdqn.servlet;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.bdqn.bean.User;
import cn.bdqn.service.UserService;
import cn.bdqn.service.impl.UserServiceImpl;

public class UserUpdateServlet extends HttpServlet {

	/**
		 * The doGet method of the servlet. <br>
		 *
		 * This method is called when a form has its tag value method equals to get.
		 * 
		 * @param request the request send by the client to the server
		 * @param response the response send by the server to the client
		 * @throws ServletException if an error occurred
		 * @throws IOException if an error occurred
		 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
		
	}

	/**
		 * The doPost method of the servlet. <br>
		 *
		 * This method is called when a form has its tag value method equals to post.
		 * 
		 * @param request the request send by the client to the server
		 * @param response the response send by the server to the client
		 * @throws ServletException if an error occurred
		 * @throws IOException if an error occurred
		 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		request.setCharacterEncoding("UTF-8");
		UserService userService = new UserServiceImpl();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		//获取用户的输入
		String idStr = request.getParameter("id");
		String userName= request.getParameter("userName");
		String genderStr = request.getParameter("gender");
		String birth= request.getParameter("data");
		String phone= request.getParameter("userphone");
		String address= request.getParameter("userAddress");
		String typeStr= request.getParameter("userlei");
		String userCode= request.getParameter("userCode");
		String password= request.getParameter("password");
		//处理数据类型不一致的数据
		int id = -1;
		if(idStr !=null  && idStr!=""){
			id = Integer.parseInt(idStr);
		}
		int gender = 0;
		if(genderStr !=null  && genderStr!=""){
			gender = Integer.parseInt(genderStr);
		}
		int typeNo = 0;
		if(typeStr !=null && typeStr!=""){
			typeNo = Integer.parseInt(typeStr);
		}
		Date birthdate = null;
		if(birth !=null && birth!=""){
			try {
				birthdate = sdf.parse(birth);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		User user = new User(userCode, password, userName, 
				gender, birthdate, phone, address, typeNo);
		user.setId(id);
		//获取修改人信息
		User u = (User) request.getSession().getAttribute("user");
		user.setModifyBy(u.getId());
		//调用修改方法
		if(userService.updateUser(user)){
			//修改成功
			response.sendRedirect("UserListServlet");
		}else{
			//修改失败
			response.sendRedirect("jsp/userUpdate.jsp");
		}		
		
		
	}

}
